$().ready(function() {
	validateRule();
});

$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});
function save() {
    var phone = $("input[name=contactMobile]").val();
    if (!/^[1][3-9][0-9]{9}$/.test(phone)) {
        parent.layer.alert("手机号码不正确");
        return;
    }
    
    var joinType = $("#joinType").val();
	if('1' == joinType){
        var userIds = '';
        $(".userDiv").each(function(){
            var id = $(this).attr("user_id");
            userIds += id +",";
        });
        if(!userIds){
            parent.layer.alert("请选择项目参与人员");
            return;
		}
		data = data +"&userIds=" + userIds;
	}
	
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx+"invest/company/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
            contactUser : {
                required : true
            },
            contactMobile : {
                required : true
            },
            address : {
                required : true
            },
            introDesc : {
                required : true
            }
		},
		messages : {
			name : {
				required : icon + "请输入名称"
			},
            contactUser : {
                required : icon + "请输入联系人"
            },
            contactMobile : {
                required : icon + "请输入联系电话"
            },
            address : {
                required : icon + "请输入地址"
            },
            introDesc : {
                required : icon + "请输入简介"
            }
		}
	})
}

function showUsers(){
    var joinType = $("#joinType").val();
    if('0' == joinType){
        $(".projectUsers").hide();
    }else{
        $(".projectUsers").show();
    }
    $(".userDiv").remove();
}

function chooseUser(){
    layer.open({
        type : 2,
        title : '选择参与人员',
        maxmin : true,
        shadeClose : false, // 点击遮罩关闭层
        area : [ '780px', '470px' ],
        content : prefix + '/chooseUser?id=', // iframe的url
		btn:['确定','取消'],
		yes:function (index) {
            var res = window["layui-layer-iframe" + index].callbackdata();
            if(res){
                $(".userDiv").remove();
				//打印返回的值，看是否有我们想返回的值。
                var html = '';
                $(res.split(";")).each(function(){
                	if(null!=this && ''!=this){
                        html += '<div class="col-sm-3 col-md-3 userDiv" user_id="'+this.split(",")[0]+'"><div class="thumbnail"><span>'+this.split(",")[1]+'</span><i class="fa fa-remove"></i></div></div>';
                    }
                });
                $(".chooseUser").before(html);
                initRemoveBtn();
                //最后关闭弹出层
                layer.close(index);
			}
        },
		cancel:function () {
            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
            parent.layer.close(index);
        }
    });
}

function initRemoveBtn() {
	$(".userDiv").click(function () {
		$(this).remove();
    });
}